package com.hzw.saas.web.admin.rbac.controller;

import java.util.List;
import javax.validation.constraints.NotNull;

import com.hzw.saas.api.rbac.IUserRoleService;
import com.hzw.saas.api.rbac.bo.PermissionTreeBO;
import com.hzw.saas.api.rbac.constants.RbacConsts;
import com.hzw.saas.api.rbac.enums.PermTypeEnum;
import com.hzw.saas.common.config.annotation.SysLog;
import com.hzw.saas.common.security.utils.SecurityUtils;
import com.hzw.saas.web.admin.rbac.param.PermissionsListParam;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import io.swagger.annotations.*;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;

/**
 * @author zzl
 * @since 09/03/2021
 */
@RestController
@Slf4j
@RequiredArgsConstructor
@Api(tags = "用户权限查询")
public class UserPermController {

    private final IUserRoleService userRoleService;

    @GetMapping("/user/role/ids")
    @SysLog
    @ApiOperation(value = "获取用户角色ID列表", notes = "获取用户当前的角色ID列表")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "sys", value = "所属系统, 1:后台管理用户, 2:云平台用户", dataType = "int", paramType = "query", required = true)
    })
    @ApiOperationSort(11)
    public ResponseEntity<List<String>> listUserPermissionIds(@NotNull(message = "所属系统不能为空") Integer sys) {
        String userId = SecurityUtils.getUser().getUserId();
        List<String> roleIds = userRoleService.listRoleIds(sys, userId);
        return ResponseEntity.ok(roleIds);
    }

    @GetMapping("/user/permissions")
    @SysLog
    @ApiOperation(value = "获取用户权限树", notes = "获取用户权限树，用户权限通用接口")
    @ApiOperationSort(12)
    public ResponseEntity<List<PermissionTreeBO>> listUserPermissions(@Validated PermissionsListParam param) {
        String userId = SecurityUtils.getUser().getUserId();
        List<PermissionTreeBO> permissionTrees = userRoleService.listPermTrees(param.getSys(),
            userId, param.getSupId(), param.getLevel(), param.getTypes());
        return ResponseEntity.ok(permissionTrees);
    }

    @GetMapping("/user/menus")
    @SysLog
    @ApiOperation(value = "获取用户菜单", notes = "获取用户菜单")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "sys", value = "所属系统, 1:后台管理用户, 2:云平台用户", dataType = "int", paramType = "query")
    })
    @ApiOperationSort(13)
    public ResponseEntity<List<PermissionTreeBO>> listUserMenus(Integer sys) {
        String userId = SecurityUtils.getUser().getUserId();
        List<PermissionTreeBO> menus = userRoleService.listPermTrees(sys, userId,
            RbacConsts.ROOT_PERMISSION_SUP_ID, -1, PermTypeEnum.MENU.getCode());
        return ResponseEntity.ok(menus);
    }

}
